package com.uhylab.uhuvinetest.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.bean.NewsInformation;
import com.uhylab.uhuvinetest.utils.VolleySingleton;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Uhylab
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/8/24 15:41
 * 修改人：xyb
 * 修改时间：16/8/24 15:41
 * 修改备注：
 */
public class UHNewsListAdapter extends BaseAdapter {
    private Context mContext;
    private List<NewsInformation.DataBean> mDataBeen = new ArrayList<>();
    private ImageLoader mImageLoader;
    private VolleySingleton volleySingleton;

    public UHNewsListAdapter(Context context) {
        mContext = context;
        volleySingleton = VolleySingleton.getVolleySingleton(context);
        mImageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public int getCount() {
        return mDataBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.activity_news_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

            //对于listview，注意添加这一行，即可在item上使用高度
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(holder.mNewsIcon, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        mImageLoader.get(mDataBeen.get(position).getTitle_images(),imageListener);
        String title = mDataBeen.get(position).getTitle();
        holder.mNewsTitle.setText(title);
        String id = mDataBeen.get(position).getId();
        String abstractX = (String) mDataBeen.get(position).getAbstractX();
        holder.mNewsThings.setText(abstractX);
        String releasetime = mDataBeen.get(position).getReleasetime();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date historyTime = df.parse(releasetime);
            long time = historyTime.getTime();
            long nowTime = System.currentTimeMillis();
            long l = (nowTime - time)/1000;

            //除以1000是为了转换成秒
            long day1=l/(24*3600);
            long hour1=l%(24*3600)/3600;
            long minute1=l%3600/60;
            long second1=l%60/60;
            if (day1 != 0) {
                holder.mNewsTime.setText(day1+"天前");
            }else if (hour1 != 0 ){
                holder.mNewsTime.setText(hour1+"小时前");

            }else if (minute1 != 0){
                holder.mNewsTime.setText(minute1+"分钟前");

            }else if (second1 != 0){
                holder.mNewsTime.setText(second1+"秒前");

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return convertView;
    }


    public void addListDate(List<NewsInformation.DataBean> mDataBeen) {
        this.mDataBeen.addAll(mDataBeen);
        notifyDataSetChanged();
    }

    public void clear() {
        this.mDataBeen.clear();
    }


    class ViewHolder {
        @BindView(R.id.news_title)
        TextView mNewsTitle;
        @BindView(R.id.news_things)
        TextView mNewsThings;
        @BindView(R.id.news_icon)
        ImageView mNewsIcon;
        @BindView(R.id.view_id)
        View mViewId;
        @BindView(R.id.news_time)
        TextView mNewsTime;
        @BindView(R.id.news_looks)
        TextView mNewsLooks;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
